//
//  MIneYBbuyViewController.m
//  TEst
//
//  Created by   丛熠超 on 2017/4/9.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "MIneYBbuyViewController.h"
#import "MineYBBuyTableViewCell.h"
#import "MineYBOperator.h"
#import "MineYBModel.h"
#import "MineYbDataModel.h"
#import "MineYbListModel.h"

@interface MIneYBbuyViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *BuyNumLB;
@property (weak, nonatomic) IBOutlet UIButton *Buy50Btn;
@property (weak, nonatomic) IBOutlet UIButton *Buy100Btn;
@property (weak, nonatomic) IBOutlet UIButton *PayBtn;
@property (weak, nonatomic) IBOutlet UITableView *MessAgeTableView;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (assign,nonatomic) int count;

@property (nonatomic, strong) NSArray *dataArray;

@end

static NSString *mainCellIdentifier = @"MainTableViewCell";

@implementation MIneYBbuyViewController

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.MessAgeTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _MessAgeTableView.delegate = self;
    _MessAgeTableView.dataSource = self;
    _MessAgeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
    [_MessAgeTableView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    _MessAgeTableView.userInteractionEnabled = NO;
    
    [self download];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)Choose50:(id)sender {
    [_Buy50Btn setBackgroundImage:[UIImage imageNamed:@"100元话费"] forState:UIControlStateNormal ];
    [_Buy100Btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
}
- (IBAction)Choose100:(id)sender {
    [_Buy50Btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal ];
    [_Buy100Btn setBackgroundImage:[UIImage imageNamed:@"100元话费"] forState:UIControlStateNormal];
    
}
- (IBAction)PayNow:(id)sender {
    NSLog(@"paynow");
}

//CADisplayLink 定时器 系统默认每秒调用60次
- (void) tick:(CADisplayLink *)displayLink {
    
    self.count ++;
    //(25.0 / 30.0) * (float)self.count) ---> (tableview需要滚动的contentOffset / 一共调用的次数) * 第几次调用
    //比如该demo中 contentOffset最大值为 = cell的高度 * cell的个数 ,5秒执行一个循环则调用次数为 300,没1/60秒 count计数器加1,当count=300时,重置count为0,实现循环滚动.
    [_MessAgeTableView setContentOffset:CGPointMake(0, ((20.0 / 45) * (float)self.count)) animated:NO];
    
    if (self.count >= 300) {
        
        self.count = 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineYBBuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"MineYBBuyTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    MineYbListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [self.displayLink invalidate];
    self.displayLink = nil;
    
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"Type"] = @"yb";
    MineYBOperator *operator = [[MineYBOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = operator.data.data.list;
            self.BuyNumLB.text = [NSString stringWithFormat:@"    已购买Y币用户: %@人", operator.data.data.all];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
